#ifndef MAINWINDOW_H
#define MAINWINDOW_H

/***************************************************************************
 *   Copyright (C) 2010 by Tobias Vogel                                    *
 *   t.vogel@linux-multimedia.ch                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License version 2        *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <cstdio>
#include <QObject>
#include <QAction>
#include <QWidget>
#include <QApplication>
#include <QLabel>
#include <QSlider>
#include <QColor>
#include <QLineEdit>
#include <QValidator>
#include <QPushButton>
#include <QClipboard>
#include <QMimeData>
#include <QString>
#include <QStringList>
#include <QMessageBox>
#include <QPainter>
#include <QPaintEvent>
#include <QStyle>
#include <QDir>
#include <QFile>
#include <QFileDialog>
#include <QSvgGenerator>
#include <QIcon>
#include <QImage>
#include <QPixmap>
#include <QFont>
#include <QFrame>
#include <QPrinter>
#include <QtXml>
#include <QDomDocument>
#include <QXmlStreamReader>
#include <QTextStream>
#include <QStringList>
#include <QMouseEvent>
#include <QMetaObject>
#include <QColorDialog>
#include <QCheckBox>
#include <QDebug>
#include <QMenuBar>
#include <QMenu>
#include "clrpick_icon.h"

using namespace std;

const QString appVersion = "2.0.11-2";

int RGB_R = 255;
int RGB_G = 255;
int RGB_B = 255;
int HSV_H = 1;
int HSV_S = 0;
int HSV_V = 255;
int CMYK_C = 0;
int CMYK_M = 0;
int CMYK_Y = 0;
int CMYK_K = 0;
int PdfPageWidth = 0;
int PdfPageHeight = 0;
int windowHeight = 520;
int yOffset = 0;
bool askSaveSetting = true;
bool saveCurrentSettings = false;
QColor color1(255,255,255);
QColor color2(255,255,255);
QColor color3(255,255,255);
QColor color4(255,255,255);
QColor color5(255,255,255);
QColor color6(255,255,255);
QString settingsPath;
QString settingsFile;
QImage *AppIcon = new QImage(16, 16, QImage::Format_ARGB32);
QImage *ClrPickIcon = new QImage(16, 16, QImage::Format_ARGB32);
QColor currentColor;

QString error;

QString HEXQString;

class dblClckLabel : public QLabel
{
	Q_OBJECT
public:
	dblClckLabel( QWidget* parent = 0 );
protected:
	void mouseDoubleClickEvent(QMouseEvent* event);
	void mouseReleaseEvent(QMouseEvent* event);
public slots:
	void askColorDeleteAction(void);
};


class mySvg : public QWidget
{
	Q_OBJECT
public:
	mySvg( QWidget *parent = 0 );
	void paint(QPainter &painter);
protected:
	void paintEvent(QPaintEvent*);
public slots:
	void selfRepaintSVG();
	void selfRepaintPDF();
	QString webSafe(void);
};

class SaveSettingsDialog : public QDialog
{
	Q_OBJECT
public:
	SaveSettingsDialog(void);

	QFrame *questionLineTop;
	QFrame *questionLineBottom;
	QLabel *questionTitle;
	QLabel *questionLabel;
	QCheckBox *showAgainCheck;
	QPushButton *yesBoxButton;
	QPushButton *noBoxButton;
	QPushButton *cancelBoxButton;
public slots:
	void reallyQuit(void);
	void toggleAskSaveSettings(void);
};


class colortoolWindow : public QWidget
{
	Q_OBJECT
public:
	colortoolWindow(void);
	void closeEvent(QCloseEvent *event);

	QMenuBar *menuBar;
	QMenu *fileMenu;
	QMenu *optionsMenu;
	QMenu *modeMenu;
	QMenu *helpMenu;
	QLabel *colorLabel;
	QLabel *websafeLabel;
	QLabel *hexColorLabel;
	QLabel *expertLabel1;
	QLabel *expertLabelHex1;
	QLabel *expertLabel2;
	QLabel *expertLabelHex2;
	QLabel *expertLabel3;
	QLabel *expertLabelHex3;
	QLabel *expertLabel4;
	QLabel *expertLabelHex4;
	QLabel *expertLabel5;
	QLabel *expertLabelHex5;
	QLabel *expertLabel6;
	QLabel *expertLabelHex6;
	QLineEdit *hexColorLine;
	QLabel *hexWebSafeLabel;
	QLabel *hexWebSafeValueLabel;
	QPushButton *copyFullHexValue;
	QPushButton *pasteFullHexValue;
	QPushButton *copyWebsafeHexValue;
	QLineEdit *RGB_R_Line;
	QLineEdit *RGB_G_Line;
	QLineEdit *RGB_B_Line;
	QLabel *r_Label;
	QLabel *g_Label;
	QLabel *b_Label;
	QSlider *r_Slider;
	QSlider *g_Slider;
	QSlider *b_Slider;
	QLabel *CMYK_C_Label;
	QLabel *CMYK_M_Label;
	QLabel *CMYK_Y_Label;
	QLabel *CMYK_K_Label;
	QLineEdit *CMYK_C_Line;
	QLineEdit *CMYK_M_Line;
	QLineEdit *CMYK_Y_Line;
	QLineEdit *CMYK_K_Line;
	QLabel *HSV_H_Label;
	QLabel *HSV_S_Label;
	QLabel *HSV_V_Label;
	QLineEdit *HSV_H_Line;
	QLineEdit *HSV_S_Line;
	QLineEdit *HSV_V_Line;
	dblClckLabel *colorSelect1;
	dblClckLabel *colorSelect2;
	dblClckLabel *colorSelect3;
	dblClckLabel *colorSelect4;
	dblClckLabel *colorSelect5;
	dblClckLabel *colorSelect6;
	QPushButton *colorSelectButton1;
	QPushButton *colorSelectButton2;
	QPushButton *colorSelectButton3;
	QPushButton *colorSelectButton4;
	QPushButton *colorSelectButton5;
	QPushButton *colorSelectButton6;
	QPushButton *colorChooseButton;
	QPushButton *clearButton;
	QPushButton *quitButton;
	QPushButton *generateSvgButton;
	QPushButton *generatePdfButton;
	QFrame *rgbLine;
	QFrame *cmykLine;
	QFrame *hsvLine;
	QFrame *selectLine;
	QFrame *buttonLine;
	QLabel *rgbLineLabel;
	QLabel *cmykLineLabel;
	QLabel *hsvLineLabel;
	QLabel *selectLineLabel;
	SaveSettingsDialog *quitDialog;
	mySvg *mySVG;
	QAction *importAction;
	QAction *exportAction;
	QAction *quitAction;
	QAction *saveBoolAction;
	QAction *askSaveBoolAction;
	QAction *setStandardModeAction;
	QAction *setExpertModeAction;
	QAction *gotoWebsiteAction;
	QAction *showAboutAction;
	QActionGroup *setModeActionsGroup;
public slots:
	void colorLabelUpdateR(int);
	void colorLabelUpdateG(int);
	void colorLabelUpdateB(int);
	void colorPicked(void);
	void fullHexChanged(void);
	void pasteFullHex(void);
	void copyWebsafeHex(void);
	void copyFullHex(void);
	QString webSafe(void);
	void updateSlider(void);
	void updateCMYK(void);
	void CMYK_C_Edited(void);
	void CMYK_M_Edited(void);
	void CMYK_Y_Edited(void);
	void CMYK_K_Edited(void);
	void RGB_R_Edited(void);
	void RGB_G_Edited(void);
	void RGB_B_Edited(void);
	void HSV_H_Edited(void);
	void HSV_S_Edited(void);
	void HSV_V_Edited(void);
	void updateHSV(void);
	void updateFromRGB(void);
	void updateFromCMYK(void);
	void updateFromHSV(void);
	void colorSelectClicked1(void);
	void colorSelectClicked2(void);
	void colorSelectClicked3(void);
	void colorSelectClicked4(void);
	void colorSelectClicked5(void);
	void colorSelectClicked6(void);
	void generateSvgCard(void);
	void generatePdfCard(void);
	void switchToExpertMode(void);
	void switchToStandardMode(void);
	void saveSettings(void);
	void restoreSettings(void);
	void restoreDeprecatedSettings(void);
	void deleteSettings(void);
	void clearSettings(void);
	void askBackupConfig(void);
	void doBackupConfig(void);
	void updateConfig(void);
	void errorAlert(void);
};

QString getRGB();
QString getHEX();
QString getCMYK();
QString getHSV();

dblClckLabel::dblClckLabel(QWidget* parent) : QLabel(parent) {
}

colortoolWindow::colortoolWindow(void) {
	this->setObjectName("colortoolWindow");

#if ! ( defined(__MINGW32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(_WIN64) || defined(__WIN64) || defined(__WINDOWS__) || defined(WINDOWS) || defined(__APPLE__) || defined(__APPLE) || defined(_APPLE) || defined(__MACH__) || defined(__MACH) || defined(_MACH) )
	settingsPath = QDir::homePath();
	settingsPath += "/.colortool";
	settingsFile = settingsPath;
	settingsFile += "/settings.xml";
#endif

#if defined(__MINGW32__) || defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(_WIN64) || defined(__WIN64) || defined(__WINDOWS__) || defined(WINDOWS)
	#define __textFont__ QFont("Arial", 10)
	#define __buttonFont__ QFont("Arial", 9)
	#define __lineFont__ QFont("Arial", 7)
	settingsPath = QCoreApplication::applicationDirPath();
	settingsFile = settingsPath;
	settingsFile += "/settings.ini";
#ifdef DEBUG
	QMessageBox::information(this, "ColorTool", "ColorTool is running under Windows", QMessageBox::Ok);
#endif
#elif defined(__APPLE__) || defined(__APPLE) || defined(_APPLE) || defined(__MACH__) || defined(__MACH) || defined(_MACH)
	#define __textFont__ QFont("Helvetica", 10)
	#define __buttonFont__ QFont("Helvetica", 9)
	#define __lineFont__ QFont("Helvetica", 8)
	windowHeight = (windowHeight - 20);
	yOffset = (-20);
	settingsPath = QDir::homePath();
	settingsPath += "/Library/Preferences";
	settingsFile = settingsPath;
	settingsFile += "/org.colortool.settings.xml.plist";
#ifdef DEBUG
	QMessageBox::information(this, "ColorTool", "ColorTool is running under Mac OS X", QMessageBox::Ok);
#endif
#elif defined(OS2) || defined(_OS2) || defined(__OS2__) || defined(__TOS_OS2__)
	#define __textFont__ QFont("Arial", 9)
	#define __buttonFont__ QFont("Arial", 9)
	#define __lineFont__ QFont("Arial", 7)
#ifdef DEBUG
	QMessageBox::information(this, "ColorTool", "ColorTool is running under OS/2", QMessageBox::Ok);
#endif
#else
	#define __textFont__ QFont("Sans", 10)
	#define __buttonFont__ QFont("Sans", 8)
	#define __lineFont__ QFont("Sans", 7)
#ifdef DEBUG
	QMessageBox::information(this, "ColorTool", "ColorTool is running under Linux", QMessageBox::Ok);
#endif
#endif

	
	this->setFixedSize(400, windowHeight);

	menuBar = new QMenuBar(0);
	menuBar->setDefaultUp(false);
	menuBar->setVisible(true);
	menuBar->setNativeMenuBar(true);
	fileMenu = new QMenu(tr("File"), menuBar);
	importAction = new QAction(fileMenu);
	importAction->setText(tr("Import Settings..."));
	exportAction = new QAction(fileMenu);
	exportAction->setText(tr("Export Settings..."));
	quitAction = new QAction(fileMenu);
	quitAction->setText(tr("Quit"));
	fileMenu->addAction(importAction);
	fileMenu->addAction(exportAction);
	fileMenu->addSeparator();
	fileMenu->addAction(quitAction);
	optionsMenu = new QMenu(tr("Options"), menuBar);
	saveBoolAction = new QAction(optionsMenu);
	saveBoolAction->setText(tr("Save on exit"));
	saveBoolAction->setCheckable(true);
	saveBoolAction->setChecked(true);
	askSaveBoolAction = new QAction(optionsMenu);
	askSaveBoolAction->setText(tr("Ask about saving on exit"));
	askSaveBoolAction->setCheckable(true);
	askSaveBoolAction->setChecked(true);
	optionsMenu->addAction(saveBoolAction);
	optionsMenu->addAction(askSaveBoolAction);
	modeMenu = new QMenu(tr("Mode"), menuBar);
	setModeActionsGroup = new QActionGroup(modeMenu);
	setModeActionsGroup->setExclusive(true);
	setStandardModeAction = new QAction(setModeActionsGroup);
	setStandardModeAction->setText(tr("Run in standard mode"));
	setStandardModeAction->setCheckable(true);
	setStandardModeAction->setChecked(true);
	setExpertModeAction = new QAction(setModeActionsGroup);
	setExpertModeAction->setText(tr("Run in expert mode"));
	setExpertModeAction->setCheckable(true);
	setExpertModeAction->setChecked(false);
	modeMenu->addAction(setStandardModeAction);
	modeMenu->addAction(setExpertModeAction);
	helpMenu = new QMenu(tr("Help"), menuBar);
	gotoWebsiteAction = new QAction(helpMenu);
	gotoWebsiteAction->setText(tr("ColorTool online..."));
	showAboutAction = new QAction(helpMenu);
	showAboutAction->setText(tr("About ColorTool"));
	helpMenu->addAction(gotoWebsiteAction);
	helpMenu->addAction(showAboutAction);
	menuBar->addMenu(fileMenu);
	menuBar->addMenu(optionsMenu);
	menuBar->addMenu(modeMenu);
	menuBar->addMenu(helpMenu);
	menuBar->setFixedWidth(this->width());

	QValidator *ValUp100 = new QIntValidator(0, 100, this);
	QValidator *ValUp255 = new QIntValidator(0, 255, this);
	QValidator *ValUp360 = new QIntValidator(0, 360, this);

	colorLabel = new QLabel(this);
        colorLabel->setGeometry(QRect(20, (40 + yOffset), 100, 100));
	colorLabel->setFrameShape(QFrame::Panel);
        colorLabel->setFrameShadow(QFrame::Sunken);
	colorLabel->setStyleSheet("background-color: rgb(255, 255, 255);");
	websafeLabel = new QLabel(this);
	websafeLabel->setGeometry(QRect(70, (90 + yOffset), 50, 50));
	websafeLabel->setFrameShape(QFrame::Panel);
	websafeLabel->setFrameShadow(QFrame::Sunken);
	websafeLabel->setStyleSheet("background-color: rgb(255, 255, 255);");
	hexColorLabel = new QLabel(this);
	hexColorLabel->setGeometry(QRect(160, (50 + yOffset), 70, 20));
	hexColorLabel->setFont(__textFont__);
	hexColorLabel->setText(tr("Hexvalue:"));
	hexColorLine = new QLineEdit(this);
	hexColorLine->setGeometry(QRect(235, (45 + yOffset), 80, 30));
	hexColorLine->setFont(__textFont__);
	hexColorLine->setText("#ffffff");
	hexColorLine->setMaxLength(7);
	hexColorLine->setInputMask("XHHHHHH");
	hexWebSafeLabel = new QLabel(this);
	hexWebSafeLabel->setGeometry(QRect(160, (110 + yOffset), 75, 20));
	hexWebSafeLabel->setFont(__textFont__);
	hexWebSafeLabel->setText(tr("Websafe:"));
	hexWebSafeValueLabel = new QLabel(this);
	hexWebSafeValueLabel->setGeometry(QRect(245, (110 + yOffset), 95, 20));
	hexWebSafeValueLabel->setFont(__textFont__);
	hexWebSafeValueLabel->setText("#ffffff");
	copyFullHexValue = new QPushButton(this);
	copyFullHexValue->setGeometry(QRect(320, (40 + yOffset), 60, 25));
	copyFullHexValue->setFont(__buttonFont__);
	copyFullHexValue->setText(tr("Copy"));
	pasteFullHexValue = new QPushButton(this);
	pasteFullHexValue->setGeometry(QRect(320, (66 + yOffset), 60, 25));
	pasteFullHexValue->setFont(__buttonFont__);
	pasteFullHexValue->setText(tr("Paste"));
	copyWebsafeHexValue = new QPushButton(this);
	copyWebsafeHexValue->setGeometry(QRect(320, (105 + yOffset), 60, 25));
	copyWebsafeHexValue->setFont(__buttonFont__);
	copyWebsafeHexValue->setText(tr("Copy"));
	r_Slider = new QSlider(this);
        r_Slider->setGeometry(QRect(20, (170 + yOffset), 256, 20));
	r_Slider->setMaximum(255);
        r_Slider->setOrientation(Qt::Horizontal);
	r_Slider->setValue(255);
	g_Slider = new QSlider(this);
        g_Slider->setGeometry(QRect(20, (200 + yOffset), 256, 20));
	g_Slider->setMaximum(255);
        g_Slider->setOrientation(Qt::Horizontal);
	g_Slider->setValue(255);
	b_Slider = new QSlider(this);
        b_Slider->setGeometry(QRect(20, (230 + yOffset), 256, 20));
	b_Slider->setMaximum(255);
        b_Slider->setOrientation(Qt::Horizontal);
	b_Slider->setValue(255);
	r_Label = new QLabel(this);
	r_Label->setGeometry(QRect(325, (170 + yOffset), 20, 20));
	r_Label->setFont(__textFont__);
	r_Label->setText("R:");
	g_Label = new QLabel(this);
	g_Label->setGeometry(QRect(325, (200 + yOffset), 20, 20));
	g_Label->setFont(__textFont__);
	g_Label->setText("G:");
	b_Label = new QLabel(this);
	b_Label->setGeometry(QRect(325, (230 + yOffset), 20, 20));
	b_Label->setFont(__textFont__);
	b_Label->setText("B:");
	RGB_R_Line = new QLineEdit(this);
	RGB_R_Line->setGeometry(QRect(347, (165 + yOffset), 35, 30));
	RGB_R_Line->setFont(__textFont__);
	RGB_R_Line->setText("255");
	RGB_R_Line->setMaxLength(3);
	RGB_R_Line->setInputMask("000");
	RGB_R_Line->setValidator(ValUp255);
	RGB_G_Line = new QLineEdit(this);
	RGB_G_Line->setGeometry(QRect(347, (195 + yOffset), 35, 30));
	RGB_G_Line->setFont(__textFont__);
	RGB_G_Line->setText("255");
	RGB_G_Line->setMaxLength(3);
	RGB_G_Line->setInputMask("000");
	RGB_G_Line->setValidator(ValUp255);
	RGB_B_Line = new QLineEdit(this);
	RGB_B_Line->setGeometry(QRect(347, (225 + yOffset), 35, 30));
	RGB_B_Line->setFont(__textFont__);
	RGB_B_Line->setText("255");
	RGB_B_Line->setMaxLength(3);
	RGB_B_Line->setInputMask("000");
	RGB_B_Line->setValidator(ValUp255);
	CMYK_C_Label = new QLabel(this);
	CMYK_C_Label->setGeometry(QRect(20, (275 + yOffset), 20, 20));
	CMYK_C_Label->setFont(__textFont__);
	CMYK_C_Label->setText("C:");
	CMYK_M_Label = new QLabel(this);
	CMYK_M_Label->setGeometry(QRect(122, (275 + yOffset), 20, 20));
	CMYK_M_Label->setFont(__textFont__);
	CMYK_M_Label->setText("M:");
	CMYK_Y_Label = new QLabel(this);
	CMYK_Y_Label->setGeometry(QRect(224, (275 + yOffset), 20, 20));
	CMYK_Y_Label->setFont(__textFont__);
	CMYK_Y_Label->setText("Y:");
	CMYK_K_Label = new QLabel(this);
	CMYK_K_Label->setGeometry(QRect(325, (275 + yOffset), 20, 20));
	CMYK_K_Label->setFont(__textFont__);
	CMYK_K_Label->setText("K:");
	CMYK_C_Line = new QLineEdit(this);
	CMYK_C_Line->setGeometry(QRect(42, (270 + yOffset), 35, 30));
	CMYK_C_Line->setFont(__textFont__);
	CMYK_C_Line->setText("0");
	CMYK_C_Line->setMaxLength(3);
	CMYK_C_Line->setInputMask("000");
	CMYK_C_Line->setValidator(ValUp255);
	CMYK_M_Line = new QLineEdit(this);
	CMYK_M_Line->setGeometry(QRect(144, (270 + yOffset), 35, 30));
	CMYK_M_Line->setFont(__textFont__);
	CMYK_M_Line->setText("0");
	CMYK_M_Line->setMaxLength(3);
	CMYK_M_Line->setInputMask("000");
	CMYK_M_Line->setValidator(ValUp255);
	CMYK_Y_Line = new QLineEdit(this);
	CMYK_Y_Line->setGeometry(QRect(246, (270 + yOffset), 35, 30));
	CMYK_Y_Line->setFont(__textFont__);
	CMYK_Y_Line->setText("0");
	CMYK_Y_Line->setMaxLength(3);
	CMYK_Y_Line->setInputMask("000");
	CMYK_Y_Line->setValidator(ValUp255);
	CMYK_K_Line = new QLineEdit(this);
	CMYK_K_Line->setGeometry(QRect(347, (270 + yOffset), 35, 30));
	CMYK_K_Line->setFont(__textFont__);
	CMYK_K_Line->setText("0");
	CMYK_K_Line->setMaxLength(3);
	CMYK_K_Line->setInputMask("000");
	CMYK_K_Line->setValidator(ValUp255);
	
	HSV_H_Label = new QLabel(this);
	HSV_H_Label->setGeometry(QRect(20, (325 + yOffset), 20, 20));
	HSV_H_Label->setFont(__textFont__);
	HSV_H_Label->setText("H:");
	HSV_S_Label = new QLabel(this);
	HSV_S_Label->setGeometry(QRect(174, (325 + yOffset), 20, 20));
	HSV_S_Label->setFont(__textFont__);
	HSV_S_Label->setText("S:");
	HSV_V_Label = new QLabel(this);
	HSV_V_Label->setGeometry(QRect(325, (325 + yOffset), 20, 20));
	HSV_V_Label->setFont(__textFont__);
	HSV_V_Label->setText("V:");
	HSV_H_Line = new QLineEdit(this);
	HSV_H_Line->setGeometry(QRect(40, (320 + yOffset), 35, 30));
	HSV_H_Line->setFont(__textFont__);
	HSV_H_Line->setMaxLength(3);
	HSV_H_Line->setInputMask("000");
	HSV_H_Line->setValidator(ValUp360);
	HSV_H_Line->setText("1");
	HSV_S_Line = new QLineEdit(this);
	HSV_S_Line->setGeometry(QRect(195, (320 + yOffset), 35, 30));
	HSV_S_Line->setFont(__textFont__);
	HSV_S_Line->setMaxLength(3);
	HSV_S_Line->setInputMask("000");
	HSV_S_Line->setText("0");
	HSV_S_Line->setValidator(ValUp255);
	HSV_V_Line = new QLineEdit(this);
	HSV_V_Line->setGeometry(QRect(347, (320 + yOffset), 35, 30));
	HSV_V_Line->setFont(__textFont__);
	HSV_V_Line->setMaxLength(3);
	HSV_V_Line->setInputMask("000");
	HSV_V_Line->setValidator(ValUp255);
	HSV_V_Line->setText("255");

	colorSelect1 = new dblClckLabel(this);
	colorSelect1->setFrameShape(QFrame::Panel);
        colorSelect1->setFrameShadow(QFrame::Sunken);
	colorSelect1->setGeometry(QRect(20, (375 + yOffset), 50, 40));
	colorSelect1->setObjectName("colorSelect1");
	colorSelect2 = new dblClckLabel(this);
	colorSelect2->setFrameShape(QFrame::Panel);
        colorSelect2->setFrameShadow(QFrame::Sunken);
	colorSelect2->setGeometry(QRect(82, (375 + yOffset), 50, 40));
	colorSelect2->setObjectName("colorSelect2");
	colorSelect3 = new dblClckLabel(this);
	colorSelect3->setFrameShape(QFrame::Panel);
        colorSelect3->setFrameShadow(QFrame::Sunken);
	colorSelect3->setGeometry(QRect(144, (375 + yOffset), 50, 40));
	colorSelect3->setObjectName("colorSelect3");
	colorSelect4 = new dblClckLabel(this);
	colorSelect4->setFrameShape(QFrame::Panel);
        colorSelect4->setFrameShadow(QFrame::Sunken);
	colorSelect4->setGeometry(QRect(206, (375 + yOffset), 50, 40));
	colorSelect4->setObjectName("colorSelect4");
	colorSelect5 = new dblClckLabel(this);
	colorSelect5->setFrameShape(QFrame::Panel);
        colorSelect5->setFrameShadow(QFrame::Sunken);
	colorSelect5->setGeometry(QRect(268, (375 + yOffset), 50, 40));
	colorSelect5->setObjectName("colorSelect5");
	colorSelect6 = new dblClckLabel(this);
	colorSelect6->setFrameShape(QFrame::Panel);
        colorSelect6->setFrameShadow(QFrame::Sunken);
	colorSelect6->setGeometry(QRect(330, (375 + yOffset), 50, 40));
	colorSelect6->setObjectName("colorSelect6");
	colorChooseButton = new QPushButton(this);
	createClrPickIcon(ClrPickIcon);
	colorChooseButton->setIcon(QPixmap(QPixmap::fromImage(*ClrPickIcon)));
	colorChooseButton->setGeometry(QRect(120, (40 + yOffset), 25, 25));
	colorSelectButton1 = new QPushButton(this);
	colorSelectButton1->setGeometry(QRect(20, (425 + yOffset), 50, 25));
	colorSelectButton1->setFont(__buttonFont__);
	colorSelectButton1->setText(tr("Select"));
	colorSelectButton2 = new QPushButton(this);
	colorSelectButton2->setGeometry(QRect(82, (425 + yOffset), 50, 25));
	colorSelectButton2->setFont(__buttonFont__);
	colorSelectButton2->setText(tr("Select"));
	colorSelectButton3 = new QPushButton(this);
	colorSelectButton3->setGeometry(QRect(144, (425 + yOffset), 50, 25));
	colorSelectButton3->setFont(__buttonFont__);
	colorSelectButton3->setText(tr("Select"));
	colorSelectButton4 = new QPushButton(this);
	colorSelectButton4->setGeometry(QRect(206, (425 + yOffset), 50, 25));
	colorSelectButton4->setFont(__buttonFont__);
	colorSelectButton4->setText(tr("Select"));
	colorSelectButton5 = new QPushButton(this);
	colorSelectButton5->setGeometry(QRect(268, (425 + yOffset), 50, 25));
	colorSelectButton5->setFont(__buttonFont__);
	colorSelectButton5->setText(tr("Select"));
	colorSelectButton6 = new QPushButton(this);
	colorSelectButton6->setGeometry(QRect(330, (425 + yOffset), 50, 25));
	colorSelectButton6->setFont(__buttonFont__);
	colorSelectButton6->setText(tr("Select"));

	clearButton = new QPushButton(this);
	clearButton->setGeometry(QRect(20, (475 + yOffset), 90, 30));
	clearButton->setFont(__buttonFont__);
	clearButton->setText(tr("&Clear All"));
	generateSvgButton = new QPushButton(this);
	generateSvgButton->setGeometry(QRect(111, (475 + yOffset), 90, 30));
	generateSvgButton->setFont(__buttonFont__);
	generateSvgButton->setText(tr("Generate &SVG"));
	generatePdfButton = new QPushButton(this);
	generatePdfButton->setGeometry(QRect(202, (475 + yOffset), 90, 30));
	generatePdfButton->setFont(__buttonFont__);
	generatePdfButton->setText(tr("Generate &PDF"));
	quitButton = new QPushButton(this);
	quitButton->setGeometry(QRect(293, (475 + yOffset), 90, 30));
	quitButton->setFont(__buttonFont__);
	quitButton->setText(tr("&Quit"));



        rgbLine = new QFrame(this);
        rgbLine->setGeometry(QRect(15, (155 + yOffset), 370, 3));
        rgbLine->setFrameShape(QFrame::HLine);
        rgbLine->setFrameShadow(QFrame::Sunken);

        cmykLine = new QFrame(this);
        cmykLine->setGeometry(QRect(15, (260 + yOffset), 370, 3));
        cmykLine->setFrameShape(QFrame::HLine);
        cmykLine->setFrameShadow(QFrame::Sunken);

        hsvLine = new QFrame(this);
        hsvLine->setGeometry(QRect(15, (310 + yOffset), 370, 3));
        hsvLine->setFrameShape(QFrame::HLine);
        hsvLine->setFrameShadow(QFrame::Sunken);

        selectLine = new QFrame(this);
        selectLine->setGeometry(QRect(15, (360 + yOffset), 370, 3));
        selectLine->setFrameShape(QFrame::HLine);
        selectLine->setFrameShadow(QFrame::Sunken);

        buttonLine = new QFrame(this);
        buttonLine->setGeometry(QRect(15, (460 + yOffset), 370, 3));
        buttonLine->setFrameShape(QFrame::HLine);
        buttonLine->setFrameShadow(QFrame::Sunken);

	rgbLineLabel = new QLabel(this);
	rgbLineLabel->setGeometry(QRect(25, (150 + yOffset), 30, 10));
	rgbLineLabel->setFont(__lineFont__);
	rgbLineLabel->setText(QString(" " + tr("RGB Section") + " "));
	rgbLineLabel->setAutoFillBackground(true);
	rgbLineLabel->adjustSize();
	cmykLineLabel = new QLabel(this);
	cmykLineLabel->setGeometry(QRect(25, (255 + yOffset), 30, 10));
	cmykLineLabel->setFont(__lineFont__);
	cmykLineLabel->setText(QString(" " + tr("CMYK Section") + " "));
	cmykLineLabel->setAutoFillBackground(true);
	cmykLineLabel->adjustSize();
	hsvLineLabel = new QLabel(this);
	hsvLineLabel->setGeometry(QRect(25, (305 + yOffset), 30, 10));
	hsvLineLabel->setFont(__lineFont__);
	hsvLineLabel->setText(QString(" " + tr("HSV Section") + " "));
	hsvLineLabel->setAutoFillBackground(true);
	hsvLineLabel->adjustSize();
	selectLineLabel = new QLabel(this);
	selectLineLabel->setGeometry(QRect(25, (355 + yOffset), 30, 10));
	selectLineLabel->setFont(__lineFont__);
	selectLineLabel->setText(QString(" " + tr("Color Selection") + " "));
	selectLineLabel->setAutoFillBackground(true);
	selectLineLabel->adjustSize();


	QObject::connect(r_Slider, SIGNAL(sliderMoved(int)), this, SLOT(colorLabelUpdateR(int)));
	QObject::connect(r_Slider, SIGNAL(valueChanged(int)), this, SLOT(colorLabelUpdateR(int)));
	QObject::connect(g_Slider, SIGNAL(sliderMoved(int)), this, SLOT(colorLabelUpdateG(int)));
	QObject::connect(g_Slider, SIGNAL(valueChanged(int)), this, SLOT(colorLabelUpdateG(int)));
	QObject::connect(b_Slider, SIGNAL(sliderMoved(int)), this, SLOT(colorLabelUpdateB(int)));
	QObject::connect(b_Slider, SIGNAL(valueChanged(int)), this, SLOT(colorLabelUpdateB(int)));
	QObject::connect(copyFullHexValue, SIGNAL(clicked()), this, SLOT(copyFullHex()));
	QObject::connect(copyWebsafeHexValue, SIGNAL(clicked()), this, SLOT(copyWebsafeHex()));
	QObject::connect(pasteFullHexValue, SIGNAL(clicked()), this, SLOT(pasteFullHex()));
	QObject::connect(hexColorLine, SIGNAL(editingFinished()), this, SLOT(fullHexChanged()));
	QObject::connect(CMYK_C_Line, SIGNAL(editingFinished()), this, SLOT(CMYK_C_Edited()));
	QObject::connect(CMYK_M_Line, SIGNAL(editingFinished()), this, SLOT(CMYK_M_Edited()));
	QObject::connect(CMYK_Y_Line, SIGNAL(editingFinished()), this, SLOT(CMYK_Y_Edited()));
	QObject::connect(CMYK_K_Line, SIGNAL(editingFinished()), this, SLOT(CMYK_K_Edited()));
	QObject::connect(RGB_R_Line, SIGNAL(editingFinished()), this, SLOT(RGB_R_Edited()));
	QObject::connect(RGB_G_Line, SIGNAL(editingFinished()), this, SLOT(RGB_G_Edited()));
	QObject::connect(RGB_B_Line, SIGNAL(editingFinished()), this, SLOT(RGB_B_Edited()));
	QObject::connect(HSV_H_Line, SIGNAL(editingFinished()), this, SLOT(HSV_H_Edited()));
	QObject::connect(HSV_S_Line, SIGNAL(editingFinished()), this, SLOT(HSV_S_Edited()));
	QObject::connect(HSV_V_Line, SIGNAL(editingFinished()), this, SLOT(HSV_V_Edited()));
	QObject::connect(colorSelectButton1, SIGNAL(clicked()), this, SLOT(colorSelectClicked1()));
	QObject::connect(colorSelectButton2, SIGNAL(clicked()), this, SLOT(colorSelectClicked2()));
	QObject::connect(colorSelectButton3, SIGNAL(clicked()), this, SLOT(colorSelectClicked3()));
	QObject::connect(colorSelectButton4, SIGNAL(clicked()), this, SLOT(colorSelectClicked4()));
	QObject::connect(colorSelectButton5, SIGNAL(clicked()), this, SLOT(colorSelectClicked5()));
	QObject::connect(colorSelectButton6, SIGNAL(clicked()), this, SLOT(colorSelectClicked6()));
	QObject::connect(colorChooseButton, SIGNAL(clicked()), this, SLOT(colorPicked()));
	QObject::connect(clearButton, SIGNAL(clicked()), this, SLOT(clearSettings()));
	QObject::connect(generateSvgButton, SIGNAL(clicked()), this, SLOT(generateSvgCard()));
	QObject::connect(generatePdfButton, SIGNAL(clicked()), this, SLOT(generatePdfCard()));
	QObject::connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
	QObject::connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
	QObject::connect(setExpertModeAction, SIGNAL(triggered()), this, SLOT(switchToExpertMode()));
	QObject::connect(setStandardModeAction, SIGNAL(triggered()), this, SLOT(switchToStandardMode()));
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);


	mySVG = new mySvg(this);
	mySVG->setGeometry(QRect(450, 20, 400, 400));

	restoreSettings();

	this->activateWindow();
	this->raise();

}

void colortoolWindow::switchToExpertMode(void) {
	this->setFixedSize(800, windowHeight);
	menuBar->setFixedWidth(this->width());
}

void colortoolWindow::switchToStandardMode(void) {
	this->setFixedSize(400, windowHeight);
	menuBar->setFixedWidth(this->width());
}


void colortoolWindow::colorLabelUpdateR(int r) {
	RGB_R = r;
	currentColor.setRgb(r, RGB_G, RGB_B);
	RGB_R_Line->setText(QString::number(currentColor.red(),10));
	colorLabel->setStyleSheet("background-color: rgb("+QString::number(currentColor.red(),10)+", "+QString::number(currentColor.green(),10)+", "+QString::number(currentColor.blue(),10)+");");
	hexColorLine->setText(currentColor.name());
	websafeLabel->setStyleSheet("background-color: " + webSafe() + ";");
	updateCMYK();
}

void colortoolWindow::colorLabelUpdateG(int g) {
	RGB_G = g;
	currentColor.setRgb(RGB_R, g, RGB_B);
	RGB_G_Line->setText(QString::number(currentColor.green(),10));
	colorLabel->setStyleSheet("background-color: rgb("+QString::number(currentColor.red(),10)+", "+QString::number(currentColor.green(),10)+", "+QString::number(currentColor.blue(),10)+");");
	hexColorLine->setText(currentColor.name());
	websafeLabel->setStyleSheet("background-color: " + webSafe() + ";");
	updateCMYK();
}

void colortoolWindow::colorLabelUpdateB(int b) {
	RGB_B = b;
	currentColor.setRgb(RGB_R, RGB_G, b);
	RGB_G_Line->setText(QString::number(currentColor.blue(),10));
	colorLabel->setStyleSheet("background-color: rgb("+QString::number(currentColor.red(),10)+", "+QString::number(currentColor.green(),10)+", "+QString::number(currentColor.blue(),10)+");");
	hexColorLine->setText(currentColor.name());
	websafeLabel->setStyleSheet("background-color: " + webSafe() + ";");
	updateCMYK();
}

void colortoolWindow::colorPicked() {
	QColorDialog colorPicker;
	currentColor = colorPicker.getColor(Qt::white, this);
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	updateSlider();
}



void colortoolWindow::updateCMYK() {
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	CMYK_C_Line->setText(QString::number(currentColor.cyan(),10));
	CMYK_M_Line->setText(QString::number(currentColor.magenta(),10));
	CMYK_Y_Line->setText(QString::number(currentColor.yellow(),10));
	CMYK_K_Line->setText(QString::number(currentColor.black(),10));
	updateHSV();
}

void colortoolWindow::updateHSV() {
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	HSV_H_Line->setText(QString::number(currentColor.hue(),10));
	HSV_S_Line->setText(QString::number(currentColor.saturation(),10));
	HSV_V_Line->setText(QString::number(currentColor.value(),10));
}

QString getCMYK() {
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	QString CMYK;
	CMYK  = (QString::number(currentColor.cyan(),10) + "," + QString::number(currentColor.magenta(),10) + "," + QString::number(currentColor.yellow(),10) + "," + QString::number(currentColor.black(),10));
	return CMYK;
}

QString getHEX() {
	return currentColor.name();
}

QString getHSV() {
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	printf("hsvSaturation: %d \n", currentColor.hsvSaturation());
	printf("hslSaturation: %d \n", currentColor.hslSaturation());
	QString HSV;
	HSV = (QString::number(currentColor.hsvHue(),10) + "," + QString::number(currentColor.hsvSaturation(),10) + "," + QString::number(currentColor.value(),10));
	return HSV;
}

QString getRGB() {
	QString rgbString;
	currentColor.setNamedColor("#"+HEXQString);
	rgbString = (QString::number(currentColor.red(),10) + "," + QString::number(currentColor.green(),10) + "," + QString::number(currentColor.blue(),10));
	return rgbString;
}

QString colortoolWindow::webSafe() {
	string RGB;
	QString HEX;
	HEX = (getHEX().right(6));
	RGB = (HEX.at(0)).toAscii();
	RGB += (HEX.at(2)).toAscii();
	RGB += (HEX.at(4)).toAscii();
    for (int ci = 0; ci < 3; ci++) {
       switch (RGB[ci]) {
         case 49:
         RGB[ci]=48;
         break;
         case 50:
         RGB[ci]=51;
         break;
         case 52:
         RGB[ci]=51;
         break;
         case 53:
         RGB[ci]=54;
         break;
         case 55:
         RGB[ci]=54;
         break;
         case 56:
         RGB[ci]=57;
         break;
         case 97:
         RGB[ci]=57;
         break;
         case 98:
         RGB[ci]=99;
         break;
         case 100:
         RGB[ci]=99;
         break;
         case 101:
         RGB[ci]=102;
         break;
      }
   }
	HEX = RGB[0];
	HEX += RGB[0];
	HEX += RGB[1];
	HEX += RGB[1];
	HEX += RGB[2];
	HEX += RGB[2];
	QString HexValue;
	HexValue = "#";
	HexValue += HEX;
	colortoolWindow::hexWebSafeValueLabel->setText(HexValue);
	return HexValue;
}


QString mySvg::webSafe() {
	string RGB;
	QString HEX;
	HEX = getHEX();
	RGB = (HEX.at(0)).toAscii();
	RGB += (HEX.at(2)).toAscii();
	RGB += (HEX.at(4)).toAscii();
    for (int ci = 0; ci < 3; ci++) {
       switch (RGB[ci]) {
         case 49:
         RGB[ci]=48;
         break;
         case 50:
         RGB[ci]=51;
         break;
         case 52:
         RGB[ci]=51;
         break;
         case 53:
         RGB[ci]=54;
         break;
         case 55:
         RGB[ci]=54;
         break;
         case 56:
         RGB[ci]=57;
         break;
         case 97:
         RGB[ci]=57;
         break;
         case 98:
         RGB[ci]=99;
         break;
         case 100:
         RGB[ci]=99;
         break;
         case 101:
         RGB[ci]=102;
         break;
      }
   }
	HEX = RGB[0];
	HEX += RGB[0];
	HEX += RGB[1];
	HEX += RGB[1];
	HEX += RGB[2];
	HEX += RGB[2];
	QString HexValue;
	HexValue = "#";
	HexValue += HEX;
	return HEX;
}


void colortoolWindow::copyFullHex() {
	QClipboard *clipboard = QApplication::clipboard();
	clipboard->setText(hexColorLine->text(), QClipboard::Clipboard);
}

void colortoolWindow::copyWebsafeHex() {
	QClipboard *clipboard = QApplication::clipboard();
	clipboard->setText(hexWebSafeValueLabel->text(), QClipboard::Clipboard);
}

void colortoolWindow::pasteFullHex() {
	const QClipboard *clipboard = QApplication::clipboard();
	const QMimeData *mimeData = clipboard->mimeData();
	QString preserveValue;
	preserveValue = hexColorLine->text();
	QString validHex;
	QString preserveValidHex;
	if (mimeData->hasText()) {
		hexColorLine->setText(clipboard->text());
	}
	if ( hexColorLine->text().length() == 6 || hexColorLine->text().length() == 7 ) {
		validHex = hexColorLine->text();
		preserveValidHex = validHex;
		if (hexColorLine->text().length() == 7) {
			validHex = validHex.right(6);	
		}
		validHex = validHex.toUpper();
		for (int i=0; i<6; i++) { validHex.replace(QString("A"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("B"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("C"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("D"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("E"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("F"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("0"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("1"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("2"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("3"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("4"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("5"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("6"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("7"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("8"), QString("")); }
		for (int i=0; i<6; i++) { validHex.replace(QString("9"), QString("")); }
		}
	HEXQString = preserveValidHex.right(6);	
	hexColorLine->setText(preserveValidHex);
	currentColor.setNamedColor(HEXQString);
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	updateSlider();
	updateCMYK();
	return;
}

void colortoolWindow::fullHexChanged() {
	QString str = hexColorLine->text();
	QString style;
	HEXQString = hexColorLine->text().right(6);
	if (str.length() == 7) {
	currentColor.setNamedColor(str);
	style = "background-color: ";
	style += hexColorLine->text();
	style += ";";
	colorLabel->setStyleSheet(style);
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	updateSlider();
	} else {
	return;
	}
}

void colortoolWindow::updateSlider() {
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	HSV_H = currentColor.hsvHue();
	HSV_S = currentColor.hsvSaturation();
	HSV_V = currentColor.value();
	CMYK_C = currentColor.cyan();
	CMYK_M = currentColor.magenta();
	CMYK_Y = currentColor.yellow();
	CMYK_K = currentColor.black();
	colorLabelUpdateR(RGB_R);
	r_Slider->setValue(RGB_R);
	colorLabelUpdateG(RGB_G);
	g_Slider->setValue(RGB_G);
	colorLabelUpdateB(RGB_B);
	b_Slider->setValue(RGB_B);
	CMYK_C_Line->setText(QString::number(CMYK_C, 10));
	CMYK_M_Line->setText(QString::number(CMYK_M, 10));
	CMYK_Y_Line->setText(QString::number(CMYK_Y, 10));
	CMYK_K_Line->setText(QString::number(CMYK_K, 10));
	HSV_H_Line->setText(QString::number(HSV_H, 10));
	HSV_S_Line->setText(QString::number(HSV_S, 10));
	HSV_V_Line->setText(QString::number(HSV_V, 10));
}

void colortoolWindow::CMYK_C_Edited() {
	CMYK_C = CMYK_C_Line->text().toInt();
	currentColor.setCmyk(CMYK_C, CMYK_M, CMYK_Y, CMYK_K);
	updateFromCMYK();

}

void colortoolWindow::CMYK_M_Edited() {
	CMYK_M = CMYK_M_Line->text().toInt();
	currentColor.setCmyk(CMYK_C, CMYK_M, CMYK_Y, CMYK_K);
	updateFromCMYK();
}


void colortoolWindow::CMYK_Y_Edited() {
	CMYK_Y = CMYK_Y_Line->text().toInt();
	currentColor.setCmyk(CMYK_C, CMYK_M, CMYK_Y, CMYK_K);
	updateFromCMYK();
}


void colortoolWindow::CMYK_K_Edited() {
	CMYK_K = CMYK_K_Line->text().toInt();
	currentColor.setCmyk(CMYK_C, CMYK_M, CMYK_Y, CMYK_K);
	updateFromCMYK();
}

void colortoolWindow::RGB_R_Edited() {
	RGB_R = RGB_R_Line->text().toInt();
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	updateFromRGB();
}

void colortoolWindow::RGB_G_Edited() {
	RGB_G = RGB_G_Line->text().toInt();
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	updateFromRGB();
}

void colortoolWindow::RGB_B_Edited() {
	RGB_B = RGB_B_Line->text().toInt();
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	updateFromRGB();
}


void colortoolWindow::HSV_H_Edited() {
	HSV_H = HSV_H_Line->text().toInt();
	currentColor.setHsv(HSV_H, HSV_S, HSV_V);
	updateFromHSV();
}

void colortoolWindow::HSV_S_Edited() {
	HSV_S = HSV_S_Line->text().toInt();
	currentColor.setHsv(HSV_H, HSV_S, HSV_V);
	updateFromHSV();
}

void colortoolWindow::HSV_V_Edited() {
	HSV_V = HSV_V_Line->text().toInt();
	currentColor.setHsv(HSV_H, HSV_S, HSV_V);
	updateFromHSV();
}

void colortoolWindow::updateFromCMYK() {
	currentColor.setCmyk(CMYK_C, CMYK_M, CMYK_Y, CMYK_K);
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	QString style;
	style = "background-color: rgb(";
	style += QString::number(currentColor.red());
	style += ", ";
	style += QString::number(currentColor.green());
	style += ", ";
	style += QString::number(currentColor.blue());
	style += ");";
	colorLabel->setStyleSheet(style);
	style = "background-color: ";
	style += webSafe();
	style += ";";
	websafeLabel->setStyleSheet(style);
	hexColorLine->setText(getHEX());
	HEXQString = hexColorLine->text().right(6);	
	updateSlider();
}

void colortoolWindow::updateFromRGB() {
	colorLabel->setStyleSheet("background-color: rgb(" + QString::number(currentColor.red(),10) + ", " + QString::number(currentColor.green(),10)+ ", " + QString::number(currentColor.blue(), 10) + ");");
	RGB_R = currentColor.red();
	RGB_G = currentColor.green();
	RGB_B = currentColor.blue();
	QString style;
	style = "background-color: ";
	style += webSafe();
	style += ";";
	websafeLabel->setStyleSheet(style);
	hexColorLine->setText(getHEX());
	HEXQString = hexColorLine->text().right(6);	
	updateSlider();
}

void colortoolWindow::updateFromHSV() {
	QColor *color = new QColor();
	if (HSV_H < 0) { HSV_H = 359; }
	color->setHsv(HSV_H, HSV_S, HSV_V);
	RGB_R = color->red();
	RGB_G = color->green();
	RGB_B = color->blue();
	updateFromRGB();
}

void dblClckLabel::mouseDoubleClickEvent(QMouseEvent *event) {
	QString StyleSheet;
	if (event->button() == Qt::LeftButton) {
		if (!(this->styleSheet() == "")) {
			StyleSheet = this->styleSheet();
			QRegExp Rx("(background-color: rgb\\(\\s?(\\d+),\\s?(\\d+),\\s?(\\d+)\\s?\\);)");
			if (Rx.indexIn(StyleSheet) >= 0) {
				RGB_R = Rx.cap(2).toInt();
				RGB_G = Rx.cap(3).toInt();
				RGB_B = Rx.cap(4).toInt();
			}
			currentColor.setRgb(RGB_R, RGB_G, RGB_B);
			QMetaObject::invokeMethod(this->parent(), "updateFromRGB", Qt::QueuedConnection);
	} else {
			event->ignore();
		}
	}

}

void dblClckLabel::askColorDeleteAction(void) {
	dblClckLabel *lHandler = this->parentWidget()->findChild<dblClckLabel *>(this->objectName());
	lHandler->setStyleSheet("");
}

void dblClckLabel::mouseReleaseEvent(QMouseEvent *event) {
	if(event->button() == Qt::RightButton) {
		if (!(this->styleSheet() == "")) {
			QMenu *popUpMenu = new QMenu(this);
			QAction *askColorDelete = new QAction(this);
			askColorDelete->setText(tr("Delete"));
			QObject::connect(askColorDelete, SIGNAL(triggered()), this, SLOT(askColorDeleteAction()));
			QColor *bgColor = new QColor(QApplication::palette().QPalette::color(QPalette::Window));
			popUpMenu->addAction(askColorDelete);
			popUpMenu->setStyleSheet("background-color: " + bgColor->name() + ";");
			popUpMenu->popup(event->globalPos());
		}
	} else {
			event->ignore();
	}
}

void colortoolWindow::colorSelectClicked1() {
	if (!(colorSelect1->styleSheet() == "") and !(colorSelect1->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect1->setStyleSheet(colorLabel->styleSheet());
	color1 = hexColorLine->text();
}
void colortoolWindow::colorSelectClicked2() {
	if (!(colorSelect2->styleSheet() == "") and !(colorSelect2->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect2->setStyleSheet(colorLabel->styleSheet());
	color2 = hexColorLine->text();
}
void colortoolWindow::colorSelectClicked3() {
	if (!(colorSelect3->styleSheet() == "") and !(colorSelect3->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect3->setStyleSheet(colorLabel->styleSheet());
	color3 = hexColorLine->text();
}
void colortoolWindow::colorSelectClicked4() {
	if (!(colorSelect4->styleSheet() == "") and !(colorSelect4->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect4->setStyleSheet(colorLabel->styleSheet());
	color4 = hexColorLine->text();
}
void colortoolWindow::colorSelectClicked5() {
	if (!(colorSelect5->styleSheet() == "") and !(colorSelect5->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect5->setStyleSheet(colorLabel->styleSheet());
	color5 = hexColorLine->text();
}
void colortoolWindow::colorSelectClicked6() {
	if (!(colorSelect6->styleSheet() == "") and !(colorSelect6->styleSheet() == colorLabel->styleSheet())) {
		QMessageBox confirm;
		confirm.setIcon(QMessageBox::Question);
		confirm.setText(tr("The selected field already holds a color."));
 		confirm.setInformativeText(tr("Do you want to replace the current color?"));
		confirm.setStandardButtons(QMessageBox::Yes | QMessageBox::Abort);
		confirm.setDefaultButton(QMessageBox::Abort);
		if (confirm.exec() != 0x00004000) {
			return;
		}
	}
	colorSelect6->setStyleSheet(colorLabel->styleSheet());
	color6 = hexColorLine->text();
}

void colortoolWindow::generateSvgCard() {
	mySVG->selfRepaintSVG();
}

void colortoolWindow::generatePdfCard() {
	mySVG->selfRepaintPDF();
}

SaveSettingsDialog::SaveSettingsDialog() {

	this->setWindowModality(Qt::ApplicationModal);
	this->setFixedSize(340, 150);
	this->setWindowTitle(tr("Quit"));
	this->move((this->x()+30),(this->y()+100));

	questionLineTop = new QFrame(this);
	questionLineTop->setGeometry(QRect(10, 15, 320, 3));
	questionLineTop->setFrameShape(QFrame::HLine);
	questionLineTop->setFrameShadow(QFrame::Sunken);

	questionLineBottom = new QFrame(this);
	questionLineBottom->setGeometry(QRect(10, 90, 320, 3));
	questionLineBottom->setFrameShape(QFrame::HLine);
	questionLineBottom->setFrameShadow(QFrame::Sunken);

	questionTitle = new QLabel(this);
	questionTitle->setGeometry(QRect(20, 10, 100, 10));
	questionTitle->setFont(__lineFont__);
	questionTitle->setText(QString(" " + tr("Quit") + " "));
	questionTitle->setAutoFillBackground(true);
	questionTitle->adjustSize();
	
	questionLabel = new QLabel(this);
	questionLabel->setGeometry(QRect(30, 25, 280, 30));
	questionLabel->setFont(__textFont__);
	questionLabel->setStyleSheet("font-weight: bold;");
	questionLabel->setText(tr("Save current color-setting?"));
	showAgainCheck = new QCheckBox(this);
	showAgainCheck->setGeometry(QRect(70, 55, 240, 30));
	showAgainCheck->setFont(__textFont__);
	showAgainCheck->setText(tr("Do not show again"));
	showAgainCheck->setObjectName("showAgainCheck");
	showAgainCheck->setLayoutDirection(Qt::RightToLeft);
	showAgainCheck->setTristate(false);
	yesBoxButton = new QPushButton(tr("&Yes"), this);
	yesBoxButton->setGeometry(QRect(10, 105, 95, 30));
	yesBoxButton->setDefault(true);
	yesBoxButton->setObjectName("yesBoxButton");
	noBoxButton = new QPushButton(tr("&No"), this);
	noBoxButton->setGeometry(QRect(120, 105, 95, 30));
	noBoxButton->setObjectName("noBoxButton");
	cancelBoxButton = new QPushButton(tr("&Cancel"), this);
	cancelBoxButton->setGeometry(QRect(230, 105, 95, 30));
	cancelBoxButton->setObjectName("cancelBoxButton");

	connect(noBoxButton, SIGNAL(clicked()), this, SLOT(reallyQuit()));
	connect(yesBoxButton, SIGNAL(clicked()), this, SLOT(reallyQuit()));
	connect(cancelBoxButton, SIGNAL(clicked()), this, SLOT(reallyQuit()));
	connect(showAgainCheck, SIGNAL(clicked()), this, SLOT(toggleAskSaveSettings()));
}


void colortoolWindow::closeEvent(QCloseEvent *event)
{

	if (askSaveSetting == false) {
		if (saveCurrentSettings == true) {
			saveSettings();
		}
		event->accept();
	} else {
		quitDialog = new SaveSettingsDialog();

		if (quitDialog->exec() == QDialog::Accepted) {
			if (saveCurrentSettings == true) {
				saveSettings();
			}
			event->accept();
		} else {
			event->ignore();
		}
	}

}

void SaveSettingsDialog::toggleAskSaveSettings() {
	if (showAgainCheck->isChecked()) {
		saveCurrentSettings = true;
		askSaveSetting = false;
	} else {
		askSaveSetting = true;
	}	
}

void SaveSettingsDialog::reallyQuit() {
	if ((this->sender())->objectName() == "yesBoxButton") {
		saveCurrentSettings = true;
		this->accept();
	} else if ((this->sender())->objectName() == "noBoxButton") {
		this->accept();
	} else {
		this->reject();
	}
}


mySvg::mySvg( QWidget *parent ) : QWidget( parent ) {
}

void mySvg::paintEvent( QPaintEvent * ) {
}

void mySvg::paint( QPainter &p ) {

	int leftSpace = 20;
	int topSpace = 1;
	
	if (PdfPageWidth != 0) {
		leftSpace = leftSpace + (PdfPageWidth/2) - 310;
	}
	if (PdfPageWidth < 0) {
		leftSpace = 1;
	}

	if (PdfPageHeight != 0) {
		topSpace = topSpace + (PdfPageHeight/2) - 250;
	}
	if (PdfPageHeight < 0) {
		topSpace = 1;
	}

	p.setBackground(QBrush(Qt::white));
	p.setPen(Qt::NoPen);
	p.setBrush(QBrush(Qt::white));
	p.drawRect(QRect(0, 0, 620, 500));
	p.setPen(QPen(Qt::black, 1));	
	p.setFont(QFont("Arial", 24));
	p.drawText(QPoint(leftSpace, (topSpace + 30)), tr("Color-Chart"));
	p.setFont(QFont("Arial", 12));
	p.setPen(QPen( Qt::black, 2));
	p.setBrush(QBrush(QColor(color1)));
	p.drawRect(QRect(leftSpace,(topSpace + 60),80,80));
	p.setBrush(QBrush(QColor(color2)));
	p.drawRect(QRect((leftSpace+100),(topSpace + 60),80,80));
	p.setBrush(QBrush(QColor(color3)));
	p.drawRect(QRect((leftSpace+200),(topSpace + 60),80,80));
	p.setBrush(QBrush(QColor(color4)));
	p.drawRect(QRect((leftSpace+300),(topSpace + 60),80,80));
	p.setBrush(QBrush(QColor(color5)));
	p.drawRect(QRect((leftSpace+400),(topSpace + 60),80,80));
	p.setBrush(QBrush(QColor(color6)));
	p.drawRect(QRect((leftSpace+500),(topSpace + 60),80,80));
	for (int i=0; i<6; i++) {
	p.drawText(QPoint(((leftSpace)+i*100),(topSpace + 185)), tr("Color:"));
	}
	p.drawText(QPoint((leftSpace),(topSpace + 200)), color1.name());
	p.drawText(QPoint((leftSpace+100),(topSpace + 200)), color2.name());
	p.drawText(QPoint((leftSpace+200),(topSpace + 200)), color3.name());
	p.drawText(QPoint((leftSpace+300),(topSpace + 200)), color4.name());
	p.drawText(QPoint((leftSpace+400),(topSpace + 200)), color5.name());
	p.drawText(QPoint((leftSpace+500),(topSpace + 200)), color6.name());
	QString preserveOriginalHex = HEXQString;
	QString RGB;
	QString HEX;
	QString CMYK;
	QString HSV;
	int preserveOriginalR = RGB_R;
	int preserveOriginalG = RGB_G;
	int preserveOriginalB = RGB_B;
	for (int i=1; i<7; i++) {
	if (i == 1) {
	HEXQString = color1.name().right(6);
	}
	if (i == 2) {
	HEXQString = color2.name().right(6);
	}
	if (i == 3) {
	HEXQString = color3.name().right(6);
	}
	if (i == 4) {
	HEXQString = color4.name().right(6);
	}
	if (i == 5) {
	HEXQString = color5.name().right(6);
	}
	if (i == 6) {
	HEXQString = color6.name().right(6);
	}
	RGB = getRGB();
	RGB_R = atoi((RGB.split(",", QString::SkipEmptyParts))[0].toStdString().c_str());
	RGB_G = atoi((RGB.split(",", QString::SkipEmptyParts))[1].toStdString().c_str());
	RGB_B = atoi((RGB.split(",", QString::SkipEmptyParts))[2].toStdString().c_str());
	HEX = webSafe();	
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 225)), tr("Websafe:"));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 240)), QString("#" + HEX));
	p.setPen(QPen( Qt::black, 1 ));
	p.setBrush(QBrush(QColor(QString("#" + HEX))));
	p.drawRect(QRect((leftSpace+50+i*100-100), (topSpace + 110), 28, 28));
	p.setPen(QPen( Qt::black, 2 ));
	p.setBrush(QBrush(QColor("#000000")));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 285)), QString("R: " + (RGB.split(",", QString::SkipEmptyParts))[0]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 300)), QString("G: " + (RGB.split(",", QString::SkipEmptyParts))[1]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 315)), QString("B: " + (RGB.split(",", QString::SkipEmptyParts))[2]));
	CMYK = getCMYK();
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 355)), QString("C: " + (CMYK.split(",", QString::SkipEmptyParts))[0]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 370)), QString("M: " + (CMYK.split(",", QString::SkipEmptyParts))[1]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 385)), QString("Y: " + (CMYK.split(",", QString::SkipEmptyParts))[2]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 400)), QString("K: " + (CMYK.split(",", QString::SkipEmptyParts))[3]));
	HSV = getHSV();
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 440)), QString("H: " + (HSV.split(",", QString::SkipEmptyParts))[0]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 455)), QString("S: " + (HSV.split(",", QString::SkipEmptyParts))[1]));
	p.drawText(QPoint((leftSpace+i*100-100), (topSpace + 470)), QString("V: " + (HSV.split(",", QString::SkipEmptyParts))[2]));
	}
	HEXQString = preserveOriginalHex;
	RGB_R = preserveOriginalR;
	RGB_G = preserveOriginalG;
	RGB_B = preserveOriginalB;
	if (PdfPageWidth != 0) {
		PdfPageWidth = 0;
	}
	if (PdfPageHeight != 0) {
		PdfPageHeight = 0;
	}
}

void mySvg::selfRepaintSVG() {
	QString path;
	QString newPath = QFileDialog::getSaveFileName(this, tr("Save SVG"),
         path, tr("SVG files (*.svg)"));

     if (newPath.isEmpty())
         return;

     path = newPath;

     if ((path.toLower()).right(4) != ".svg")
	path += ".svg";

     QSvgGenerator generator;
     generator.setFileName(path);
     generator.setSize(QSize(620, 500));
     generator.setViewBox(QRect(0, 0, 620, 500));
     generator.setTitle(tr("SVG Color Chart"));
     generator.setDescription(tr("An SVG Color Chart generated by ColorTool."));
     QPainter painter;
     painter.begin(&generator);
     this->paint(painter);
     painter.end();

}

void mySvg::selfRepaintPDF() {
	QString path;
	QString newPath = QFileDialog::getSaveFileName(this, tr("Save PDF"),
         path, tr("PDF files (*.pdf)"));

     if (newPath.isEmpty())
         return;

     path = newPath;

     if ((path.toLower()).right(4) != ".pdf")
	path += ".pdf";

     QPrinter generator;
     generator.setOutputFileName(path);
     generator.setOutputFormat(QPrinter::PdfFormat);
     generator.setOrientation(QPrinter::Landscape);
     generator.setDocName(tr("PDF Color Chart"));
     PdfPageWidth = QRect(generator.pageRect()).width();
     PdfPageHeight = QRect(generator.pageRect()).height();
     QPainter painter;
     painter.begin(&generator);
     this->paint(painter);
     painter.end();

}

void colortoolWindow::saveSettings() {

	QDir *saveDir = new QDir(settingsPath);
	if (!(saveDir->exists())) {
		if(!saveDir->mkdir(settingsPath)) {
			error = tr("Error: Saving-path could not be created. Settings not saved.");
			errorAlert();
		}
	}

	QString RGB;
	
	QDomDocument doc("settings");
	QDomElement root = doc.createElement("settings");
	doc.appendChild(root);
	QDomElement mySettings;
	QDomAttr settingsId;
	QDomElement myColorR;
	QDomElement myColorG;
	QDomElement myColorB;
	for (int i = 0; i < 7; i++) {
	if (i == 0) {
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("current");
	}
	if (i == 1) {
		if (colorSelect1->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color1.name().right(6);
			RGB_R = color1.red();
			RGB_G = color1.green();
			RGB_B = color1.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("1");
	}
	if (i == 2) {
		if (colorSelect2->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color2.name().right(6);
			RGB_R = color2.red();
			RGB_G = color2.green();
			RGB_B = color2.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("2");
	}
	if (i == 3) {
		if (colorSelect3->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color3.name().right(6);
			RGB_R = color3.red();
			RGB_G = color3.green();
			RGB_B = color3.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("3");
	}
	if (i == 4) {
		if (colorSelect4->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color4.name().right(6);
			RGB_R = color4.red();
			RGB_G = color4.green();
			RGB_B = color4.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("4");
	}
	if (i == 5) {
		if (colorSelect5->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color5.name().right(6);
			RGB_R = color5.red();
			RGB_G = color5.green();
			RGB_B = color5.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("5");
	}
	if (i == 6) {
		if (colorSelect6->styleSheet() == "") {
			RGB_R = -1;
		} else {
			HEXQString = color6.name().right(6);
			RGB_R = color6.red();
			RGB_G = color6.green();
			RGB_B = color6.blue();
		}
		mySettings = doc.createElement("setting");
		myColorR = doc.createElement("color");
		myColorG = doc.createElement("color");
		myColorB = doc.createElement("color");
		settingsId = doc.createAttribute("id");
		settingsId.setValue("6");
	}
	mySettings.setAttributeNode(settingsId);
	root.appendChild(mySettings);
	QDomAttr colorTypeR = doc.createAttribute("type");
	colorTypeR.setValue("red");
	QDomAttr colorTypeG = doc.createAttribute("type");
	colorTypeG.setValue("green");
	QDomAttr colorTypeB = doc.createAttribute("type");
	colorTypeB.setValue("blue");
	mySettings.appendChild(myColorR);
	mySettings.appendChild(myColorG);
	mySettings.appendChild(myColorB);
	QDomText redValue;
	QDomText greenValue;
	QDomText blueValue;
	if (RGB_R >= 0) {
		redValue = doc.createTextNode(QString::number(RGB_R, 10));
	} else {
		redValue = doc.createTextNode("-1");
	}
	if (RGB_R >= 0) {
		greenValue = doc.createTextNode(QString::number(RGB_G, 10));
	} else {
		greenValue = doc.createTextNode("-1");
	}
	if (RGB_R >= 0) {
		blueValue = doc.createTextNode(QString::number(RGB_B, 10));
	} else {
		blueValue = doc.createTextNode("-1");
	}
	myColorR.setAttributeNode(colorTypeR);
	myColorG.setAttributeNode(colorTypeG);
	myColorB.setAttributeNode(colorTypeB);
	myColorR.appendChild(redValue);
	myColorG.appendChild(greenValue);
	myColorB.appendChild(blueValue);
	}

	// General application settings

	QDomAttr myConfig;
	mySettings = doc.createElement("setting");
	myConfig = doc.createAttribute("id");
	myConfig.setValue("general");
	mySettings.setAttributeNode(myConfig);
	root.appendChild(mySettings);
	QDomElement myConfigItem = doc.createElement("config");
	QDomAttr configVersion = doc.createAttribute("value");
	configVersion.setValue("version");
	mySettings.appendChild(myConfigItem);
	QDomText myVersion = doc.createTextNode(appVersion);
	myConfigItem.setAttributeNode(configVersion);
	myConfigItem.appendChild(myVersion);
	QDomElement mySaveOnQuit = doc.createElement("config");
	QDomAttr SaveOnQuit = doc.createAttribute("value");
	SaveOnQuit.setValue("saveonquit");
	mySettings.appendChild(mySaveOnQuit);
	QDomText mySaveOnQuitValue;
	if (saveCurrentSettings == true) {
		mySaveOnQuitValue = doc.createTextNode("true");
	} else {
		mySaveOnQuitValue = doc.createTextNode("false");
	}
	mySaveOnQuit.setAttributeNode(SaveOnQuit);
	mySaveOnQuit.appendChild(mySaveOnQuitValue);

	QDomElement asksave = doc.createElement("config");
	QDomAttr configAskSave = doc.createAttribute("value");
	configAskSave.setValue("asksave");
	myConfigItem = doc.createElement("config");
	mySettings.appendChild(myConfigItem);
	QDomText askSave;
	if (askSaveSetting == 0) {
		askSave = doc.createTextNode("false");
	} else {
		askSave = doc.createTextNode("true");
	}
	myConfigItem.setAttributeNode(configAskSave);
	myConfigItem.appendChild(askSave);


	QString xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
	xml += doc.toString();

	QFile saveFile(settingsFile);
	if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
		error = tr("Error: Saving-file could not be created. Settings not saved.");
		errorAlert();
	} else {
	QTextStream out(&saveFile);
	out << xml;
	saveFile.flush();
	saveFile.close();
	}
	
}

void colortoolWindow::deleteSettings() {

	QFile loadFile(settingsFile);

	if( !loadFile.exists()) {
		return;
	}
	
	if(loadFile.remove()) {
		return;
	} else {
		error = tr("Error: Could not delete saved settings.");
		errorAlert();
	}
	return;
}

void colortoolWindow::doBackupConfig() {

		QString saveFileName = QFileDialog::getSaveFileName(this, tr("Save as..."), (QDir::homePath() + "/colortool-backup.xml"), tr("XML-Configuration (*.xml)"));

		QFile saveFileHandle(saveFileName);

		while (saveFileName == "" || (!saveFileHandle.open(QIODevice::WriteOnly | QIODevice::ReadWrite))) {
			QMessageBox fileOperationError;
			fileOperationError.setWindowTitle("ColorTool");
			fileOperationError.setIcon(QMessageBox::Warning);
			fileOperationError.setText(tr("Either no valid filename was chosen or the folder/device is not writable."));
			fileOperationError.setInformativeText(tr("Do you want to try again?"));
			fileOperationError.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
			fileOperationError.setDefaultButton(QMessageBox::Yes);
			if (fileOperationError.exec() != 0x00004000) {
				saveFileHandle.close();
				updateConfig();
				return;
			} else {
				saveFileName = QFileDialog::getSaveFileName(this, tr("Save as..."), (QDir::homePath() + "/colortool-backup.xml"), tr("XML-Configuration (*.xml)"));
				saveFileHandle.setFileName(saveFileName);
			}
		}

		saveFileHandle.remove();

		if(!QFile::copy(settingsFile, saveFileName)) {
			error = "File could not be saved.";
			errorAlert();
		}
		saveFileHandle.close();
	
		updateConfig();
}

void colortoolWindow::restoreDeprecatedSettings() {

	QFile loadFile(settingsFile);

	loadFile.open( QIODevice::ReadOnly );

	loadFile.reset();

	QXmlStreamReader xml(&loadFile);

	QList<QString> xmlList;

	while (!xml.atEnd() && !xml.hasError()) {
		QXmlStreamReader::TokenType token = xml.readNext();
		if (token == QXmlStreamReader::StartDocument) {
			continue;
		}
		if (xml.name() == "setting" && xml.attributes().hasAttribute("id")) {
			QXmlStreamAttributes attributes = xml.attributes();
			xmlList.append(attributes.value("id").toString());
		}
		if (xml.name() == "color") {
			QXmlStreamAttributes attributes = xml.attributes();
			xmlList.append(xml.readElementText());
		}
		
	}


	if (xml.hasError()) {
		error = tr("Error: Failed to parse settings-file. Previous settings could not be restored." );
		errorAlert();
	return;
	}
	
	loadFile.close();

	if (xmlList.count() < 5) {
		error = tr("Error: Failed to parse settings-file. Previous settings could not be restored." );
		errorAlert();
	return;		
	}

	for (int i = 4; i < xmlList.count(); i++) {
		int colornumber;
		if (QString("123456").indexOf(xmlList.at(i)) >= 0) {
			colornumber = atoi(xmlList.at(i).toStdString().c_str());
		} else {
			i++;
			continue;
		}
		QString StyleSheet = "background-color: rgb(";
		i++;
		StyleSheet += xmlList.at(i);
		RGB_R = atoi(xmlList.at(i).toStdString().c_str());
		StyleSheet += ", ";
		i++;
		StyleSheet += xmlList.at(i);
		RGB_G = atoi(xmlList.at(i).toStdString().c_str());
		StyleSheet += ", ";
		i++;
		StyleSheet += xmlList.at(i);
		RGB_B = atoi(xmlList.at(i).toStdString().c_str());
		StyleSheet += ");";
		QString restoreHEX = getHEX();
		switch(colornumber)
		{
		case 1:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color1 = restoreHEX;
			colorSelect1->setStyleSheet(StyleSheet);
		break;
		case 2:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color2 = restoreHEX;
			colorSelect2->setStyleSheet(StyleSheet);
		break;
		case 3:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color3 = restoreHEX;
			colorSelect3->setStyleSheet(StyleSheet);
		break;
		case 4:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color4 = restoreHEX;
			colorSelect4->setStyleSheet(StyleSheet);
		break;
		case 5:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color5 = restoreHEX;
			colorSelect5->setStyleSheet(StyleSheet);
		break;
		case 6:
			if (StyleSheet == "background-color: rgb(-1, -1, -1);") {
				break;
			}
			color6 = restoreHEX;
			colorSelect6->setStyleSheet(StyleSheet);
		break;
		default:
		break;
		}
		i++;
	}
	RGB_R = atoi(xmlList.at(1).toStdString().c_str());
	RGB_G = atoi(xmlList.at(2).toStdString().c_str());
	RGB_B = atoi(xmlList.at(3).toStdString().c_str());
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	updateFromRGB();
}


void colortoolWindow::updateConfig() {
		restoreDeprecatedSettings();
		saveSettings();
}

void colortoolWindow::askBackupConfig() {
		QMessageBox configUpdate;
		configUpdate.setWindowTitle("ColorTool");
		configUpdate.setIcon(QMessageBox::Question);
		configUpdate.setText(tr("Your previously saved ColorTool configuration seems to be derived from an older version of ColorTool.\n\nThe older configuration will now be converted to the current configuration-format.\n\nIf you prefer to backup your deprecated configuration in advance, you can save it now.\n"));
 		configUpdate.setInformativeText(tr("Do you want to save your old configuration file?"));
		configUpdate.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
		configUpdate.setDefaultButton(QMessageBox::Yes);
		if (configUpdate.exec() != 0x00004000) {
			updateConfig();
		} else {
			doBackupConfig();
		}
}



void colortoolWindow::restoreSettings() {

	QFile loadFile(settingsFile);

	if( !loadFile.exists()) {
		return;
	}


	if( !loadFile.open( QIODevice::ReadOnly ) ) {
		error = tr("Error: Failed to load settings-file. Previous settings could not be restored." );
		errorAlert();
    		return;
	}

	loadFile.reset();

	QXmlStreamReader xml(&loadFile);

	QList<QString> xmlList;
	QList<QString> xmlConfig;

	while (!xml.atEnd() && !xml.hasError()) {
		QXmlStreamReader::TokenType token = xml.readNext();
		if (token == QXmlStreamReader::StartDocument) {
			continue;
		}
		if (xml.name() == "setting" && xml.attributes().hasAttribute("id")) {
			QXmlStreamAttributes attributes = xml.attributes();
			xmlList.append("id=" + attributes.value("id").toString());
		}
		if (xml.name() == "color") {
			QXmlStreamAttributes attributes = xml.attributes();
			xmlList.append(xml.readElementText());
		}
		if (xml.name() == "config") {
			QXmlStreamAttributes attributes = xml.attributes();
			xmlConfig.append(xml.readElementText());
		}
		
	}
	if (xml.hasError()) {
		error = tr("Error: Failed to parse settings-file. Previous settings could not be restored." );
		errorAlert();
	return;
	}
	
	loadFile.close();

	if (xmlList.count() < 5) {
		error = tr("Error: Failed to parse settings-file. Previous settings could not be restored." );
		errorAlert();
	return;		
	}

	if (xmlConfig.count() <= 0) {
		loadFile.close();
		askBackupConfig();
	}

	for (int i = 0; i < xmlList.count(); i++) {
		bool ok;
		if (xmlList.at(i) == "id=current") {
			i++;
			RGB_R = (xmlList.at(i).toInt(&ok, 10));
			if (ok == false) { RGB_R = 255; }
			i++;
			RGB_G = (xmlList.at(i).toInt(&ok, 10));
			if (ok == false) { RGB_G = 255; }
			i++;
			RGB_B = (xmlList.at(i).toInt(&ok, 10));
			if (ok == false) { RGB_B = 255; }
			i++;
		}
		int colornumber, valR, valG, valB;
		char rgbStyleSheet[50];
		if (xmlList.at(i) == "id=general") { break; }
		if (xmlList.at(i) == "id=1" ||
		    	xmlList.at(i) == "id=2" ||
			xmlList.at(i) == "id=3" ||
			xmlList.at(i) == "id=4" ||
			xmlList.at(i) == "id=5" ||
			xmlList.at(i) == "id=6" ) {
			colornumber = (QString(xmlList.at(i).at(3)).toInt(&ok, 10));
			if (ok == false) { continue; } 
		} else {
			continue;
		}
		switch(colornumber)
		{
		case 1:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect1->setStyleSheet(rgbStyleSheet);
			color1.setRgb(valR, valG, valB);
		break;
		case 2:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect2->setStyleSheet(rgbStyleSheet);
			color2.setRgb(valR, valG, valB);
		break;
		case 3:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect3->setStyleSheet(rgbStyleSheet);
			color3.setRgb(valR, valG, valB);
		break;
		case 4:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect4->setStyleSheet(rgbStyleSheet);
			color4.setRgb(valR, valG, valB);
		break;
		case 5:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect5->setStyleSheet(rgbStyleSheet);
			color5.setRgb(valR, valG, valB);
		break;
		case 6:
			valR = xmlList.at(i+1).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valG = xmlList.at(i+2).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			valB = xmlList.at(i+3).toInt(&ok, 10);
			if (ok == false) {
				break;
			}
			if ( valR < 0 || valG < 0 || valB < 0) { break; }
			sprintf(rgbStyleSheet, "background-color: rgb(%d, %d, %d);", valR, valG, valB);
			colorSelect6->setStyleSheet(rgbStyleSheet);
			color6.setRgb(valR, valG, valB);
		break;
		default:
		break;
		}
		i++;
	}
	currentColor.setRgb(RGB_R, RGB_G, RGB_B);
	updateFromRGB();
}

void colortoolWindow::clearSettings() {
	RGB_R = 255;
	RGB_G = 255;
	RGB_B = 255;
	updateFromRGB();
	colorSelect1->setStyleSheet("");
	colorSelect2->setStyleSheet("");
	colorSelect3->setStyleSheet("");
	colorSelect4->setStyleSheet("");
	colorSelect5->setStyleSheet("");
	colorSelect6->setStyleSheet("");
	QMessageBox msgBox;
	QPushButton *yesBoxButton = msgBox.addButton(tr("&Yes"), QMessageBox::ActionRole);
	QPushButton *noBoxButton = msgBox.addButton(tr("&No"), QMessageBox::ActionRole);
	QPushButton *cancelBoxButton = msgBox.addButton(tr("&Cancel"), QMessageBox::ActionRole);
	
	msgBox.setIcon(QMessageBox::Question);
	msgBox.setText(tr("Also delete previously saved values?"));

	msgBox.exec();

	if (msgBox.clickedButton() == yesBoxButton) {
		deleteSettings();
	} else if (msgBox.clickedButton() == noBoxButton) {
	} else if (msgBox.clickedButton() == cancelBoxButton) {
	}
	return;
}


void colortoolWindow::errorAlert() {
	QMessageBox::warning( this, "ColorTool", error );
}


#include "moc_mainwindow.cpp"
#endif // MAINWINDOW_H
